<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/3/8
 * Time: 12:48
 */

namespace app\index\model;

use think\Model;

class Mch extends Model
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';

    public function addNew($uid='',$name='',$tel='',$password='',$company='',$proxy_id=0,&$out=''){
        $count = $this
            ->where('uid','eq',$uid)
            ->count('id');
        if($count){
            $out= '您已经申请了，请耐心等待审核';
            return false;
        }
        $data = [
            'uid'=>$uid,
            'name'=>$name,
            'username'=>$name,
            'tel'=>$tel,
            'password'=>$password,
            'company'=>$company,
            'proxy_id'=>$proxy_id,
            'create_time'=>time()
        ];
        $validate = new \app\index\validate\Mch();
        if(!$validate->check($data)){
            $out = $validate->getError();
            return false;
        }
        if(!$this->insert($data)){
            $out = '申请失败';
            return false;
        }
        return true;
    }

    /**
     * 获取数据
     * @param int $id
     * @param string $field
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getListByProxyId($id=0,$field='*',$limit=10){
        $list = $this
            ->field($field)
            ->where('proxy_id','eq',$id)
        ->paginate($limit);
        return $list;
    }
}